package com.tzs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tzs.dao.UserNewMapper;
import com.tzs.enums.StatusCode;
import com.tzs.model.Role;
import com.tzs.model.Student;
import com.tzs.model.User;
import com.tzs.model.UserExport;
import com.tzs.response.BaseResponse;
import com.tzs.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 用户业务类
 *
 * @author Tanzs
 * @Date 2019/8/20
 */
@Service
public class UserServiceImpl implements UserService {

    private static final Logger log= LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserNewMapper userNewMapper;

    @Override
    public BaseResponse getAllUser(int pageNo, int PageSize, User user) {
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try {
            PageHelper.startPage(pageNo, PageSize);
            List<User> users=userNewMapper.getAllUser(user);
            PageInfo info=new PageInfo<User>(users);
            response.setCount(info.getTotal());
            response.setData(info.getList());
        }catch (Exception e){
            log.error("查询用户信息业务实现逻辑-发生异常：",e.fillInStackTrace());
            response=new BaseResponse(StatusCode.Fail);
        }
        return response;
    }

    @Override
    public BaseResponse delUserById(String ids) {
        BaseResponse response=new BaseResponse(StatusCode.DeleteSucess);
        try{
            String [] id=ids.split(",");
            int flag=userNewMapper.delUserById(id);
            if (flag==0){
                response=new BaseResponse(StatusCode.UserNoExit);
            }
        }catch (Exception e){
            log.error("查询用户信息业务实现逻辑-发生异常：",e.fillInStackTrace());
            response=new BaseResponse(StatusCode.DeleteError);
        }
        return response;
    }

    @Override
    @Transactional(rollbackFor = Exception.class) //异常便数据回滚
    public BaseResponse insertUser(User user,Integer rid) {
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = sdf.format(new Date());
            user.setRecentlylanded(date);
           int userFlag=userNewMapper.insertSelective(user);
           int roleFlag=userNewMapper.insertRole(user.getId(),rid);
            if (userFlag<=0||roleFlag<=0){
               response=new BaseResponse(StatusCode.Fail);
            }
        }catch (Exception e){
            log.error("新增用户信息业务实现逻辑-发生异常：",e.fillInStackTrace());
            response=new BaseResponse(StatusCode.Fail);
        }
        return response;
    }

    @Override
    public BaseResponse updateUser(User user) {
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try{
            int flag=userNewMapper.updateByPrimaryKeySelective(user);
            if (flag==0){
                response=new BaseResponse(StatusCode.Fail);
            }
        }catch (Exception e){
            log.error("修改用户信息业务实现逻辑-发生异常：",e.fillInStackTrace());
            response=new BaseResponse(StatusCode.Fail);
        }
        return response;
    }

    @Override
    public BaseResponse getExecelList() {
       BaseResponse response=new BaseResponse(StatusCode.Success);
       try{
           List<UserExport> list=userNewMapper.getExecelList();
           response.setCount(list.size());
           response.setData(list);

       }catch (Exception e){
           log.error("用户信息表格导出业务发生异常",e.fillInStackTrace());
           response=new BaseResponse(StatusCode.Fail);
       }
       return response;
    }

    @Override
    public BaseResponse Test() {
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try{
            List<Student> paramList=new ArrayList<>();
            Student r=new Student();
            r.setId(1);
            r.setName("张三");
            r.setAge("25");
            Student r2=new Student();
            r2.setId(2);
            r2.setName("李四");
            r2.setAge("20");
            paramList.add(r);
            paramList.add(r2);
            userNewMapper.Test(paramList);
        }catch (Exception e){
            log.error("用户信息表格导出业务发生异常",e.fillInStackTrace());
            response=new BaseResponse(StatusCode.Fail);
        }
        return response;
    }

   /* public List<UserExport> UpdateList(List<UserExport> list){
        List<UserExport> newList=new ArrayList<>();
        if (list!=null&&list.size()==0){
            for(UserExport user :list){
                if (user.getGender().equals("male")){
                    user.setGender("男");
                }else if (user.getGender().equals("woman")){
                    user.setGender("女");
                }
                newList.add(user);
            }
        }
        return newList;
    }*/
}